﻿using System;
using System.IO;
using System.Reflection;

namespace Mono.Cecil.Cil
{
	// Token: 0x02000153 RID: 339
	internal static class SymbolProvider
	{
		// Token: 0x06000C45 RID: 3141 RVA: 0x00035D68 File Offset: 0x00033F68
		private static AssemblyName GetSymbolAssemblyName(SymbolKind kind)
		{
			bool flag = kind == SymbolKind.PortablePdb;
			if (flag)
			{
				throw new ArgumentException();
			}
			string symbolNamespace = SymbolProvider.GetSymbolNamespace(kind);
			AssemblyName name = typeof(SymbolProvider).Assembly.GetName();
			AssemblyName assemblyName = new AssemblyName
			{
				Name = name.Name + "." + symbolNamespace,
				Version = name.Version,
				CultureInfo = name.CultureInfo
			};
			assemblyName.SetPublicKeyToken(name.GetPublicKeyToken());
			return assemblyName;
		}

		// Token: 0x06000C46 RID: 3142 RVA: 0x00035DEC File Offset: 0x00033FEC
		private static Type GetSymbolType(SymbolKind kind, string fullname)
		{
			Type type = Type.GetType(fullname);
			bool flag = type != null;
			Type result;
			if (flag)
			{
				result = type;
			}
			else
			{
				AssemblyName symbolAssemblyName = SymbolProvider.GetSymbolAssemblyName(kind);
				type = Type.GetType(fullname + ", " + symbolAssemblyName.FullName);
				bool flag2 = type != null;
				if (flag2)
				{
					result = type;
				}
				else
				{
					try
					{
						Assembly assembly = Assembly.Load(symbolAssemblyName);
						bool flag3 = assembly != null;
						if (flag3)
						{
							return assembly.GetType(fullname);
						}
					}
					catch (FileNotFoundException)
					{
					}
					catch (FileLoadException)
					{
					}
					result = null;
				}
			}
			return result;
		}

		// Token: 0x06000C47 RID: 3143 RVA: 0x00035E90 File Offset: 0x00034090
		public static ISymbolReaderProvider GetReaderProvider(SymbolKind kind)
		{
			bool flag = kind == SymbolKind.PortablePdb;
			ISymbolReaderProvider result;
			if (flag)
			{
				result = new PortablePdbReaderProvider();
			}
			else
			{
				bool flag2 = kind == SymbolKind.EmbeddedPortablePdb;
				if (flag2)
				{
					result = new EmbeddedPortablePdbReaderProvider();
				}
				else
				{
					string symbolTypeName = SymbolProvider.GetSymbolTypeName(kind, "ReaderProvider");
					Type symbolType = SymbolProvider.GetSymbolType(kind, symbolTypeName);
					bool flag3 = symbolType == null;
					if (flag3)
					{
						throw new TypeLoadException("Could not find symbol provider type " + symbolTypeName);
					}
					result = (ISymbolReaderProvider)Activator.CreateInstance(symbolType);
				}
			}
			return result;
		}

		// Token: 0x06000C48 RID: 3144 RVA: 0x00035F04 File Offset: 0x00034104
		private static string GetSymbolTypeName(SymbolKind kind, string name)
		{
			return string.Concat(new string[]
			{
				"Mono.Cecil.",
				SymbolProvider.GetSymbolNamespace(kind),
				".",
				kind.ToString(),
				name
			});
		}

		// Token: 0x06000C49 RID: 3145 RVA: 0x00035F50 File Offset: 0x00034150
		private static string GetSymbolNamespace(SymbolKind kind)
		{
			bool flag = kind == SymbolKind.PortablePdb || kind == SymbolKind.EmbeddedPortablePdb;
			string result;
			if (flag)
			{
				result = "Cil";
			}
			else
			{
				bool flag2 = kind == SymbolKind.NativePdb;
				if (flag2)
				{
					result = "Pdb";
				}
				else
				{
					bool flag3 = kind == SymbolKind.Mdb;
					if (!flag3)
					{
						throw new ArgumentException();
					}
					result = "Mdb";
				}
			}
			return result;
		}
	}
}
